package br.com.news.dao;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import br.com.news.dao.interfaces.INoticiaDao;
import br.com.news.dao.util.GenericDao;
import br.com.news.exception.DataException;
import br.com.news.modal.CategoriaBean;
import br.com.news.modal.NoticiaBean;
import br.com.news.util.Constantes;
import br.com.news.util.DataUtil;

public class NoticiaDao extends GenericDao<NoticiaBean> implements INoticiaDao {

	public NoticiaDao(Context context) {
		super(context);
	}

	@Override
	public NoticiaBean preencheAtributos(Cursor c) {

		int idxIdNot = c.getColumnIndex(NoticiaBean.ID_NOTICIA);
		int idxIdCat = c.getColumnIndex(NoticiaBean.ID_CATEGORIA);
		int idxFonte = c.getColumnIndex(NoticiaBean.FONTE);
		int idxAutor = c.getColumnIndex(NoticiaBean.AUTOR);
		int idxTexto = c.getColumnIndex(NoticiaBean.TEXTO);
		int idxTitulo = c.getColumnIndex(NoticiaBean.TITULO);
		int idxDtNot = c.getColumnIndex(NoticiaBean.DATA_NOTICIA);

		NoticiaBean noticiaBean = new NoticiaBean();
		noticiaBean.setIdNoticia(c.getLong(idxIdNot));

		CategoriaBean categoriaBean = new CategoriaBean();
		categoriaBean.setIdCategoria(c.getLong(idxIdCat));
		noticiaBean.setCategoria(categoriaBean);

		noticiaBean.setDescricaoFonte(c.getString(idxFonte));
		noticiaBean.setAutor(c.getString(idxAutor));
		noticiaBean.setTexto(c.getString(idxTexto));
		noticiaBean.setTitulo(c.getString(idxTitulo));

		try {
			noticiaBean.setDataNoticia(DataUtil.parse(c.getString(idxDtNot)));
		} catch (ParseException e) {
			Log.e(Constantes.LOG, String.format("<NoticiaDao> Falha ao realizar o parse da data Noticia %d!", noticiaBean.getId()), e);
		}

		return noticiaBean;
	}

	@Override
	public ContentValues preencheValores(NoticiaBean entity) {

		ContentValues values = new ContentValues();
		values.put(NoticiaBean.ID_NOTICIA, entity.getIdNoticia());
		values.put(NoticiaBean.ID_CATEGORIA, entity.getCategoria().getIdCategoria());
		values.put(NoticiaBean.AUTOR, entity.getAutor());
		values.put(NoticiaBean.FONTE, entity.getDescricaoFonte());
		values.put(NoticiaBean.TEXTO, entity.getTexto());
		values.put(NoticiaBean.TITULO, entity.getTitulo());
		values.put(NoticiaBean.DATA_NOTICIA, DataUtil.format(entity.getDataNoticia()));

		return values;
	}

	@Override
	public List<NoticiaBean> preencheAtributosList(Cursor c) {

		int idxIdNot = c.getColumnIndex(NoticiaBean.ID_NOTICIA);
		int idxIdCat = c.getColumnIndex(NoticiaBean.ID_CATEGORIA);
		int idxFonte = c.getColumnIndex(NoticiaBean.FONTE);
		int idxAutor = c.getColumnIndex(NoticiaBean.AUTOR);
		int idxTexto = c.getColumnIndex(NoticiaBean.TEXTO);
		int idxTitulo = c.getColumnIndex(NoticiaBean.TITULO);
		int idxDtNot = c.getColumnIndex(NoticiaBean.DATA_NOTICIA);

		List<NoticiaBean> noticiaBeans = new ArrayList<NoticiaBean>();

		if (c.getCount() > 0) {
			do {

				NoticiaBean noticiaBean = new NoticiaBean();
				noticiaBean.setIdNoticia(c.getLong(idxIdNot));

				CategoriaBean categoriaBean = new CategoriaBean();
				categoriaBean.setIdCategoria(c.getLong(idxIdCat));
				noticiaBean.setCategoria(categoriaBean);

				noticiaBean.setDescricaoFonte(c.getString(idxFonte));
				noticiaBean.setAutor(c.getString(idxAutor));
				noticiaBean.setTexto(c.getString(idxTexto));
				noticiaBean.setTitulo(c.getString(idxTitulo));

				try {
					noticiaBean.setDataNoticia(DataUtil.parse(c.getString(idxDtNot)));
				} catch (ParseException e) {
					Log.e(Constantes.LOG, String.format("<NoticiaDao> Falha ao realizar o parse da data Noticia %d!", noticiaBean.getId()), e);
				}

				noticiaBeans.add(noticiaBean);

			} while (c.moveToNext());
		}

		return noticiaBeans;
	}

	@Override
	public List<NoticiaBean> findNoticiaByIdCategoria(long idCategoria) throws DataException {
		try {

			String where = String.format("%s = ? ", NoticiaBean.ID_CATEGORIA);
			String[] whereArgs = new String[] { String.valueOf(idCategoria) };
			String orderBy = String.format("%s DESC", NoticiaBean.ID_NOTICIA);

			return findWhere(new NoticiaBean(), where, whereArgs, orderBy);

		} catch (Exception e) {
			throw new DataException(String.format("<NoticiaDao> Falha ao buscar noticias pela categoria: %d", idCategoria), e);
		}
	}

	@Override
	public NoticiaBean buscaUltimaNoticia(long idCategoria) throws DataException {
		try {

			String where = String.format("%s = ? ", NoticiaBean.ID_CATEGORIA);
			String[] whereArgs = new String[] { String.valueOf(idCategoria) };
			String orderBy = String.format("%s DESC", NoticiaBean.ID_NOTICIA);

			List<NoticiaBean> noticiaBeans = findWhere(new NoticiaBean(), where, whereArgs, orderBy);
			if (!noticiaBeans.isEmpty()) {
				return noticiaBeans.get(0);
			}
			
			return null;

		} catch (Exception e) {
			throw new DataException(String.format("<NoticiaDao> Falha ao buscar ultima noticia da categoria: %d", idCategoria), e);
		}
	}

}
